package me.numark.fragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.marshalchen.ultimaterecyclerview.ui.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.numark.R;
import me.numark.model.LikeListResponse;
import me.numark.model.TopicListResponse;
import me.numark.model.UserListResponse;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.view.UserGridAdapter;

/**
 * Created by Evan on 15/7/24.
 */
public class UserGridFragment extends BaseListFragment {

    private static final String ARG_TYPE = "type";
    private static final String ARG_ID = "labelId";
    private int labelType;
    private long labelId;

    public static final int TYPE_LABEL_LIKE = 1;

    UserGridAdapter mUserAdapter;
   // VerticalSwipeRefreshLayout swipeRefreshLayout;
    /*public void setRefreshLayout(VerticalSwipeRefreshLayout refreshLayout){
        swipeRefreshLayout = refreshLayout;
    }*/

    public static UserGridFragment newInstance(int feedType, long labelId) {
        UserGridFragment fragment = new UserGridFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_TYPE, feedType);
        args.putLong(ARG_ID, labelId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            labelType = getArguments().getInt(ARG_TYPE, -1);
            labelId = getArguments().getLong(ARG_ID, 0);
        }
        dataList = new ArrayList();
        mUserAdapter = new UserGridAdapter(dataList, this);
        mAdapter = mUserAdapter;
        if (labelType == TYPE_LABEL_LIKE) {
            empty_text = R.string.empty_likes;
        }
    }

    @Override
    public void initUI(View view) {
        super.initUI(view);
        toolbar.setVisibility(View.GONE);
        int padding = getResources().getDimensionPixelSize(R.dimen.default_padding);
        recyclerView.setPadding(padding, padding / 2, padding, padding / 2);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 8);
        recyclerView.setLayoutManager(manager);
        recyclerView.setDefaultOnRefreshListener(null);
        recyclerView.mSwipeRefreshLayout.setColorSchemeResources(R.color.btn_green);
        recyclerView.mSwipeRefreshLayout.setEnabled(false);
        recyclerView.mSwipeRefreshLayout.setRefreshing(false);
    //    recyclerView.mSwipeRefreshLayout = swipeRefreshLayout;
        recyclerView.mSwipeRefreshLayout.setEnabled(false);
        recyclerView.setDefaultOnRefreshListener(this);
    }

    @Override
    public void loadMore(int itemsCount, int maxLastVisiblePosition) {
        super.loadMore(itemsCount, maxLastVisiblePosition);
        loadData(itemsCount);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        loadData(0);
    }

    protected String getLabelLike() {
        switch (labelType) {
            case TYPE_LABEL_LIKE:
                //TODO 修改此URL 获取对标签点赞的列表
                //return String.format(NetworkConstants.TOPIC_LIKES_URL, Global.getCurrentUid(), feedId);
                return String.format(NetworkConstants.GET_LIKES,labelId);
           /* case TYPE_MARK_FORWRAD:
                return String.format(NetworkConstants.TOPIC_FORWARD_URL, Global.getCurrentUid(), feedId);*/
            default:
                return null;
        }
    }

    @Override
    protected BaseResponse parseData(String data) {
        Gson gson = Global.getGson();
        return gson.fromJson(data, TopicListResponse.class);
    }

    @Override
    protected void addDataToList(BaseResponse response, boolean isNew) {
        LikeListResponse resp = (LikeListResponse) response;
        if (isNew) {
            mUserAdapter.clear();
        }
        mUserAdapter.append(resp.data);
        setHasMore(resp.has_more);
    }

    protected void loadData(final int start) {
        if (needLogin && Global.currentUser == null) {
            endRefreshing();
            showEmptyView();
            return;
        }
        new RequestServerTask<LikeListResponse>(LikeListResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("start", String.valueOf(start));
                params.put("limit", String.valueOf(Constants.LARGE_PAGE_SIZE));
                if (start > 0 && lastResponseTime > 0) {
                    params.put("time", String.valueOf(lastResponseTime));
                }
                return HttpUtil.get(getLabelLike(), params);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                endRefreshing();
            }

            @Override
            protected void onSuccess(LikeListResponse result) {
                bindData(result, start == 0);
            }
        }
                .setSnackbarView(rootView)
                .start();
    }

}
